<template>
  <el-container class="home-ioc">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/4.png" alt="" />
        <span>小崔的后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <!-- 左边 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="backDown">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409Eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="aPaths"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单模版 -->
            <template slot="title">
              <!-- 图标  -->
              <i :class="iconSobj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="cohooseApath(subitem.path)"
            >
              <!-- 二级菜单模版 -->
              <template slot="title">
                <!-- 图标  -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 右边 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created: function () {
    //  在页面渲染渲染前调用 获取菜单
    this.getMenuList()
    // 拿出本地地址渲染给aPath 从而高亮
    this.aPaths = window.sessionStorage.getItem('active')
  },
  data () {
    return {
      // 创建一个空数组
      menuList: [],
      // 一级菜单 图标
      iconSobj: {
        125: 'iconfont  icon-user',
        103: 'iconfont  icon-tijikongjian',
        101: 'iconfont  icon-shangpin',
        102: 'iconfont  icon-danju',
        145: 'iconfont  icon-baobiao'
      },
      // 菜单折叠
      isCollapse: false,
      // 获取高亮的地址
      aPaths: ''
    }
  },
  methods: {
    // 菜单高亮
    cohooseApath (apath) {
      // 存入本地地址
      window.sessionStorage.setItem('active', apath)
      this.aPaths = apath
    },
    logout () {
      this.$messageBox.confirm('确定退出吗', {
        confirmButtonText: '确定~~',
        callback: (action) => {
          if (action == 'confirm') {
            // 清控 token值
            window.sessionStorage.clear()
            // 跳转login页面
            this.$router.push('/login')
          }
        }
      })
    },
    // 获取菜单
    async getMenuList () {
      const { data: resData } = await this.$http.get('menus')
      // 成功后吧渲染的数据 放到数组里面
      if (resData.meta.status == 200) {
        this.menuList = resData.data
      }
    },
    backDown () {
      // 设置点击 改变菜单折叠 状态
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>

<style lang="less" scoped>
.home-ioc {
  height: 100%;
}
.el-header {
  background-color: #363c42;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #363c42;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #e9ecf1;
  background: url('../assets/3.jpg') center no-repeat;
}
.icon-fonts {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
